<template>
  <div class="case">
    <div class="printBox">
      <printCode ref="printCode" :data-arr="inStockForm.inStockBillMxList" />
    </div>
    <na-table
      :datas.sync="tableData"
      :searchObj.sync="search"
      @updateDatas="getAllLists"
    >
      <template #slot1>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="newAddInstockBill"
          >申请单入库</el-button
        >
        <el-button
          type="primary"
          @click="handleOtherInstockBill"
          size="small"
          icon="el-icon-plus"
          class="mr10"
          >其他入库</el-button
        >
      </template>
    </na-table>
    <!-- 采购新建入库 -->
    <el-dialog
      :title="'物料入库-' + modelTitle"
      :visible.sync="isAddPurDialog"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="inStockForm"
            :model="inStockForm"
            label-width="80px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="入库单号" prop="inStockBillNo">
                  <el-input
                    v-model="inStockForm.inStockBillNo"
                    placeholder="保存后自动生成"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="创建人员" prop="associationBillNo">
                  <el-input
                    :value="inStockForm.createorName || name"
                    placeholder="创建人员"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="associationBillNo">
                  <el-input
                    :value="inStockForm.createTime || getDate()"
                    placeholder="创建日期"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="入库类型" prop="inStockType">
                  <el-select
                    filterable
                    v-model="inStockForm.inStockType"
                    placeholder="请选择入库类型"
                    size="small"
                    disabled
                  >
                    <el-option
                      :label="item.objectName"
                      :value="item.objectCode"
                      v-for="(item, index) in inStockTypeList"
                      :key="index"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col
                :span="8"
                v-if="inStockForm.inStockType != 'InStockType5'"
              >
                <el-form-item label="申请单号" prop="associationBillNo">
                  <el-input
                    v-model="inStockForm.associationBillNo"
                    placeholder="申请单号"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col
                :span="8"
                v-if="inStockForm.inStockType != 'InStockType5'"
              >
                <el-form-item label="申请人员" prop="requestUser">
                  <el-input
                    v-model="inStockForm.requestUser"
                    placeholder="申请人员"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="备注" class="inlineText">
                  <el-input
                    v-model="inStockForm.inStockBillRemark"
                    type="textarea"
                    :rows="3"
                    :disabled="disabled"
                    placeholder="备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">采购明细</span>
              <el-button
                size="mini"
                class="mr6"
                type="primary"
                @click="handlePrintCode"
                >打印标签</el-button
              >
            </div>
            <el-table
              :data="inStockForm.inStockBillMxList"
              :row-key="(row) => row.rowKey"
              
              border
              style="width: 100%"
            >
              <el-table-column type="index" label="序号" width="50" />
              <el-table-column
                prop="goodsCode"
                width="150"
                align="center"
                label="规格编号"
              >
                <template slot-scope="scope">
                  {{ scope.row.specificationCode || "--" }}
                </template>
              </el-table-column>
              <el-table-column
                prop="materialType"
                width="150"
                align="center"
                label="物料类型"
              >
                <template slot-scope="scope">
                  {{ scope.row.goodsType || "--" }}
                </template>
              </el-table-column>
              <el-table-column
                prop="school"
                width="150"
                align="center"
                label="供应商"
              >
                <template slot-scope="scope">
                  <el-select
                    filterable
                    v-model="scope.row.supplierCode"
                    placeholder="请选择供应商"
                    :disabled="
                      inStockForm.inStockType != 'InStockType5' || disabled
                    "
                    size="small"
                    @change="supplierChange(scope.row, $event)"
                  >
                    <el-option
                      :key="item.supplierID"
                      :label="item.supplierName"
                      :value="item.supplierCode"
                      v-for="(item, index) in allSupliers"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="school"
                width="150"
                align="center"
                label="物料名称"
              >
                <template slot-scope="scope">
                  <el-select
                    filterable
                    v-model="scope.row.goodsCode"
                    placeholder="请选择物料"
                    size="small"
                    :disabled="
                      inStockForm.inStockType != 'InStockType5' || disabled
                    "
                    @change="
                      handleMaterialNameChange(scope.row, scope.$index, $event)
                    "
                  >
                    <el-option
                      :label="item.goodsName"
                      :value="item.goodsCode"
                      v-for="(item, index) in scope.row.materialList"
                      :key="index"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="specificationCode"
                width="150"
                align="center"
                label="规格型号"
              >
                <template slot-scope="scope">
                  <el-select
                    filterable
                    :disabled="
                      inStockForm.inStockType != 'InStockType5' || disabled
                    "
                    v-model="scope.row.specificationCode"
                    @change="handleSpecificationChange(scope.row, scope.$index)"
                    placeholder="请选择规格型号"
                    size="small"
                  >
                    <el-option
                      :label="item.specificationName"
                      :value="item.specificationCode"
                      v-for="(item, index) in scope.row.goodsSpecificationList"
                      :key="index"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="productionBatchCode"
                width="150"
                align="center"
                label="生产批次"
              >
                <template slot-scope="scope">
                  <customInput
                    :disabled="disabled"
                    v-if="isShow"
                    v-model="scope.row.productionBatchCode"
                    :sourceData="scope.row.productionBatchList"
                    @childValueChange="
                      handleProductionBatchChange(scope.row, $event)
                    "
                    @handleSaveSelect="
                      handleSaveProductionBatch(scope.row, $event,scope.$index)
                    "
                  />
                  <!-- <el-select
                    v-else
                    :disabled="disabled"
                    v-model="scope.row.productionBatchNumber"
                    @change="handleBatchChange(scope.row, $event)"
                    filterable
                    value-key="productionBatchCode"
                    placeholder="请选择生产批次"
                    size="small"
                  >
                    <el-option
                      :label="item.productionBatchNumber"
                      :value="item"
                      v-for="(item, index) in scope.row.productionBatchList"
                      :key="index"
                    />
                  </el-select> -->
                </template>
              </el-table-column>
              <el-table-column
                prop="goodsUnit"
                width="180"
                align="center"
                label="生产日期"
              >
                <template slot-scope="{ row }">
                  <el-date-picker
                    v-model="row.productionDate"
                    :disabled="disabled"
                    type="date"
                    style="width: 170px"
                    value-format="yyyy-MM-dd"
                    size="small"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </template>
              </el-table-column>
              <el-table-column
                prop="machineCode"
                width="150"
                align="center"
                label="机台名称"
                v-if="inStockForm.inStockType == 'InStockType2'"
              >
                <template scope="{row}">
                  <el-select
                    v-model="row.machineCode"
                    :disabled="disabled"
                    placeholder="请选择"
                    size="small"
                    filterable
                  >
                    <el-option
                      v-for="item in allMaterielClass"
                      :key="item.machineCode"
                      :label="item.machineName"
                      :value="item.machineCode"
                    >
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="requestQuantity"
                width="150"
                align="center"
                label="申请数量"
              >
              </el-table-column>
              <el-table-column
                prop="inStockQuantity"
                width="150"
                align="center"
                label="入库数量"
              >
                <template slot-scope="{ row }">
                  <el-input
                    :disabled="disabled"
                    v-model="row.inStockQuantity"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="priceUnit"
                width="150"
                align="center"
                label="使用单位"
              >
              </el-table-column>
              <el-table-column
                label="入库库位"
                show-overflow-tooltip
                prop="stockArea"
                min-width="150"
              >
                <template slot-scope="{ row }">
                  <el-cascader
                    :disabled="disabled"
                    v-model="row.stockArea"
                    :show-all-levels="false"
                    size="mini"
                    :options="storageLocations"
                    :props="{ label: 'name', value: 'code' }"
                  />
                </template>
              </el-table-column>
              <el-table-column prop="inStockRemark" align="center" label="备注">
                <template slot-scope="{ row }">
                  <el-input
                    :disabled="disabled"
                    v-model="row.inStockRemark"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                width="100px"
                fixed="right"
                align="center"
                v-if="!disabled"
              >
                <template slot-scope="scope">
                  <el-button
                    v-if="
                      scope.$index != inStockForm.inStockBillMxList.length - 1
                    "
                    type="text"
                    @click="handleCopyRow(scope.row, scope.$index)"
                    >复制</el-button
                  >
                  <el-button
                    type="text"
                    v-if="
                      inStockForm.inStockBillMxList.length > 1 &&
                      scope.$index != inStockForm.inStockBillMxList.length - 1
                    "
                    @click="handleDeleteRow(scope.row, scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddPurDialog = false">取 消</el-button>
        <!-- 同意      -->
        <el-button
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          v-if="this.state != 'see'"
          @click="savaAdd"
          >仅保存</el-button
        >
        <el-button
          :loading="addPurLoading"
          :disabled="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          v-if="this.state !== 'see'"
          @click="handleSavaToStorage"
          >保存并入库</el-button
        >
      </div>
    </el-dialog>

    <!-- 申请单明细 -->
    <el-dialog
      title="选择申请单"
      :visible.sync="isPurInfo"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
    >
      <div class="dialogDody">
        <!-- 搜索     -->
        <div class="btnCon flex flex_between" style="border-bottom: none">
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="purSearch.likeWords"
              prefix-icon="el-icon-search"
              placeholder="输入关键字"
              size="small"
              @keyup.enter.native="getPurInfo"
            >
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon lastTdNoBorder pt0" style="padding: 16px">
          <el-table
            :data="purInfoLists"
            @row-dblclick="confirmSelecd"
            height="550"
            border
            style="width: 100%"
          >
            <el-table-column
              type="index"
              label="序号"
              align="center"
              width="50"
            />
            <el-table-column
              prop="associationBillNo"
              label="申请单号"
              min-width="110"
            />
            <el-table-column
              prop="billTypeName"
              label="单据类型"
              min-width="100"
            />
            <el-table-column prop="detailCount" label="明细" min-width="100">
              <template slot-scope="scope">
                <div class="cellcontent">
                  <el-popover placement="bottom" width="600" trigger="click">
                    <el-table
                      :data="detail"
                      height="300"
                      v-loading="detailLoading"
                      size="mini"
                    >
                      <el-table-column
                        width="180"
                        property="specificationCode"
                        label="规格编号"
                      ></el-table-column>
                      <el-table-column
                        width="100"
                        property="goodsType"
                        label="物料类型"
                      ></el-table-column>
                      <el-table-column
                        width="180"
                        property="goodsName"
                        label="物料名称"
                      ></el-table-column>
                      <el-table-column
                        width="150"
                        property="specificationName"
                        label="规格型号"
                      >
                      </el-table-column>
                      <el-table-column
                        width="200"
                        property="supplierName"
                        label="供应商"
                      ></el-table-column>
                      <el-table-column
                        width="150"
                        property="requestQuantity"
                        label="数量"
                      ></el-table-column>
                      <el-table-column
                        width="150"
                        property="goodsUnit"
                        label="使用单位"
                      ></el-table-column>
                    </el-table>
                    <span
                      slot="reference"
                      style="color: #409eff; cursor: pointer"
                      @click="handleDetailClick(scope.row)"
                      >{{ scope.row.detailCount }}</span
                    >
                  </el-popover>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="remark"
              label="备注"
              min-width="150"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="申请时间"
              min-width="90"
            />
            <el-table-column prop="creator" label="申请人员" min-width="90" />
          </el-table>
          <div class="paginaCon noborder">
            <pagination
              :total="total || 0"
              :page.sync="purSearch.pageIndex"
              :limit.sync="purSearch.pageSize"
              @pagination="getPurInfo"
            />
          </div>
        </div>
      </div>
    </el-dialog>

    <!-- 拒绝原因   -->
    <el-dialog
      :title="title + '原因'"
      :visible.sync="reasonVisible"
      :close-on-click-modal="false"
      custom-class="comDialog"
      append-to-body
      width="30%"
      @close="closeCancelDialog"
    >
      <div class="dialogDody">
        <el-form
          ref="cancelForm"
          :model="inStockState"
          :rules="cancelRules"
          label-width="0px"
          class="dialogForm"
        >
          <el-form-item label="" prop="reason">
            <el-input
              v-model="inStockState.reason"
              type="textarea"
              :autosize="{ minRows: 3 }"
              placeholder="请输入内容"
              size="mini"
              clearable
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="reasonVisible = false">取 消</el-button>
        <el-button
          :loading="addLableLoading"
          :disabled="addLableLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveReason"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
  <script>
import naTable from "@/components/naTable";
import Pagination from "@/components/Pagination";
import { arraySort } from "@/utils";
import printCode from "./printCode.vue";
import { mapGetters } from "vuex";
import customInput from "@/components/customInput";
export default {
  name: "Warehousing",
  components: {
    Pagination,
    naTable,
    customInput,
    printCode,
  },
  computed: {
    ...mapGetters(["name"]),
    modelTitle: function () {
      let title = "";
      if (this.state == "add") {
        title = "新增";
      } else if (this.state == "edit") {
        title = "编辑";
      } else if (this.state == "see") {
        title = "查看";
      } else if (this.state == "confirm") {
        title = "确认";
      } else if (this.state == "withdraw") {
        title = "撤回";
      } else if (this.state == "warehousing") {
        title = "入库";
      }
      return title;
    },
  },
  data() {
    return {
      ops: "add", // 表格操作
      state: "",
      isShow:true,
      addPurLoading: false,
      isPurInfo: false, // 采购单明细
      disabled: false,
      inStockBillState: "",
      isAddPurDialog: false, // 新建采购弹框是否打开
      isSeePurDialog: false, // 查看入库详情
      searchLoading: false, // 列表搜索加载效果
      detailLoading: false, //申请单明细加载效果
      marLoading: false, // 物料明细加载效果
      inStockForm: {
        inStockBillNo: "系统自动生成",
        inStockType: "采购入库",
        associationBillNo: "",
        billNo: "",
        requestUserCode: "",
        requestUser: "",
        inStockBillRemark: "",
        inStockType: "",
        inStockBillMxList: [],
      }, // 新建入库表单

      inStockState: {
        inStockBillNo: "",
        inStockBillState: 0,
        reason: "",
      },
      title: "",
      reasonVisible: false,
      addLableLoading: false,
      cancelRules: {
        reason: [
          {
            required: true,
            message: "请填写原因",
            trigger: ["change", "blur"],
          },
        ],
      },

      inStockBillDetailList: [], // 入库明细数据
      purInfoLists: [], // 采购明细列表
      allMaterielClass: [], //机台
      storageLocations: [], // 库位列表
      inStockID: null, // 入库单ID
      // 入库列表搜索条件
      search: {
        pageIndex: 1,
        pageSize: 30,
        queryDataBaseFiled: "",
        likeWords: "",
        isShowDisabled: 0,
        inStockBillState: 0,
      },
      inStockTypeList: [], //入库类型
      detail: [],
      total: 0,
      warhouseList: [], // 仓库下拉列表数据
      materialList: [], // 物料明细列表
      materalSearch: {
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
      }, // 物料明细搜索条件
      purSearch: {
        pageIndex: 1,
        pageSize: 30,
        queryDataBaseFiled: "",
        likeWords: "",
        isShowDisabled: 0,
        inStockBillState: 0,
      }, // 申请单明细搜索条件
      marTotale: 0,
      currentRow: {},

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "inStockID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        addTitle: "", //新建按钮名称-为''则不显示，为undefine则为新建
        addPermission: "MenuCode5-2-1-1", //新建按钮权限
        addIcon: "", //新建按钮icon
        voidWord: "", //需要查询作废的字段
        statusList: [], //状态筛选条件列表
        statusWord: "", //状态筛选的字段
        refresh: true, //刷新按钮
        showSum: true, //显示合计
        //表格按钮
        operationBtn: [
          {
            title: "查看", //按钮名称
            methodsName: "seeInsock", //按钮绑定方法的字段
          },
          {
            title: "编辑", //按钮名称
            methodsName: "handleEdit", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "inStockBillState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          // {
          //   title: "确认", //按钮名称
          //   methodsName: "handleConfirm", //按钮绑定方法的字段
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "inStockBillState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       "待确认",
          //     ],
          //   },
          // },
          // {
          //   title: "撤回", //按钮名称
          //   methodsName: "handleWithdraw", //按钮绑定方法的字段
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "inStockBillState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       "待入库",
          //     ],
          //   },
          // },
          // {
          //   title: "入库", //按钮名称
          //   methodsName: "handleWarehousing", //按钮绑定方法的字段
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "inStockBillState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用-值必须跟状态的类型保持一致
          //       "待入库",
          //     ],
          //   },
          // },
          {
            title: "作废", //按钮名称
            methodsName: "handleVoid", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "inStockBillState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "红冲", //按钮名称
            methodsName: "handleRedflush", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "isFlush", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                0,
              ],
            },
          },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },
    };
  },
  created() {
    // 采购列表表格头部
    this.getTableHead();
    // 获取仓库下拉列表
    this.getWarehouseList();
    // 库区库位
    this.getStorageLocations();

    //获取供应商
    this.getSupplier();

    //入库类型
    this.getInStockType();

    //获取设备
    this.getMaterielClass();
  },
  mounted() {},
  methods: {
    // 供应商选择改变事件
    supplierChange(row, val) {
      row.goodsCode = "";
      row.specificationCode = "";
      row.productionBatchCode = "";
      row.materialList = [];
      row.goodsSpecificationList = [];
      row.goodsType = "";
      this.$api.mater
        .GetGoodsSpecificationBySupplierCode({
          queryBy: 0,
          queryValue: val,
        })
        .then((res) => {
          row.materialList = res;
        });
      this.$forceUpdate();
    },

    // 获取供应商
    getSupplier() {
      this.$api.mater.GetAllSuppliers().then((res) => {
        this.allSupliers = res;
      });
    },

    //打印标签
    handlePrintCode() {
      this.$print(this.$refs.printCode, {
        onPrint: ".noPrint",
        onStart: () => {
          console.log("打印开始");
        },
        onEnd: () => {
          console.log("打印完成");
        },
      });
    },

    // 获取所有设备
    getMaterielClass() {
      this.$api.product
        .GetMatchProductDevices({
          likeWords: "",
          machineState: 1,
        })
        .then((res) => {
          this.allMaterielClass = res;
        });
    },

    closeCancelDialog() {
      this.inStockState.reason = "";
    },

    //物料下拉选择事件
    handleMaterialNameChange(row, index, e) {
      console.log(row);
      if (index == this.inStockForm.inStockBillMxList.length - 1) {
        this.inStockForm.inStockBillMxList.push({
          supplierCode: "",
          goodsCode: "",
          specificationCode: "",
          productionBatchCode: "",
          machineCode: "",
          goodsPrice: 0,
          inStockQuantity: 0,
          materialList: [],
          warehouseAreaCode: "",
          areaShelfCode: "",
          inStockRemark: "",
          rowKey: Date.now(),
        });
      }
      row.stockGoodsCode = e;
      row.specificationCode = "";
      row.materialList.forEach((item, i) => {
        if (item.goodsCode == e) {
          row.materialType = item.categoryName;
          row.materialName = item.goodsName;
          row.goodsSpecificationList = item.goodsSpecificationList;
          // row.goodsUnit = item.goodsUnit;
          row.goodsType = item.goodsType;
          row.priceUnit = item.goodsUnit;
        }
      });
    },

    //规格型号选择事件
    handleSpecificationChange(row, index) {
      this.inStockForm.inStockBillMxList.forEach((item) => {
        if (
          item.goodsSpecificationList &&
          item.goodsSpecificationList.length > 0
        ) {
          item.goodsSpecificationList.forEach((x) => {
            if (item.specificationCode == x.specificationCode) {
              item.productionBatchList = x.productionBatchList;
              if (item.productionBatchList.length > 0) {
                item.productionBatchList.forEach((k) => {
                  k.label = k.productionBatchNumber;
                  k.value = k.productionBatchCode;
                });
              }
              item.goodsPrice = x.goodsPrice;
            }
          });
        }
      });
      this.$forceUpdate();
    },

    //编辑
    handleEdit(row) {
      this.state = "edit";
      this.disabled = false;
      this.getStockBillInfo(row.inStockBillNo);
    },

    // 查看采购入库
    seeInsock(row) {
      this.state = "see";
      this.disabled = true;
      this.getStockBillInfo(row.inStockBillNo);
    },

    //确认
    handleConfirm(row) {
      this.state = "confirm";
      this.disabled = true;
      this.inStockState.inStockBillNo = row.inStockBillNo;
      this.getStockBillInfo(row.inStockBillNo);
    },

    //撤回
    handleWithdraw(row) {
      this.state = "withdraw";
      this.disabled = true;
      this.inStockState.inStockBillNo = row.inStockBillNo;
      this.getStockBillInfo(row.inStockBillNo);
    },

    //入库
    handleWarehousing(row) {
      this.state = "warehousing";
      this.disabled = true;
      this.inStockState.inStockBillNo = row.inStockBillNo;
      this.getStockBillInfo(row.inStockBillNo);
    },

    //作废
    handleVoid(row) {
      this.title = "作废";
      this.inStockState.inStockBillNo = row.inStockBillNo;
      this.inStockState.inStockBillState = 5;
      this.reasonVisible = true;
    },

    //保存并入库
    handleSavaToStorage() {
      if(this.state == 'add'){
        this.$refs.inStockForm.validate((valid) => {
        if (valid) {
          this.addPurLoading = true;
          let isvalid = true;

          if (this.inStockForm.inStockType == "InStockType5") {
            let arr = JSON.parse(
              JSON.stringify(this.inStockForm.inStockBillMxList)
            );
            if (arr.length > 1) {
              arr.pop();
              arr.forEach((item) => {
                if (item.stockArea) {
                  item.warehouseAreaCode = item.stockArea[0];
                  item.areaShelfCode = item.stockArea[1];
                  if (item.stockArea.length < 1) {
                    isvalid = false;
                    return;
                  }
                } else {
                  isvalid = false;
                }
              });
            }
            this.inStockForm.inStockBillMxList = JSON.parse(
              JSON.stringify(arr)
            );
          } else {
            this.inStockForm.inStockBillMxList.forEach((item) => {
              if (item.stockArea) {
                item.warehouseAreaCode = item.stockArea[0];
                item.areaShelfCode = item.stockArea[1];
                if (item.stockArea.length < 1) {
                  isvalid = false;
                  return;
                }
              } else {
                isvalid = false;
              }
            });
          }
          if (!isvalid) {
            this.$message({
              message: "请选择入库库位",
              type: "warning",
            });
            this.addPurLoading = false;
            if (this.inStockForm.inStockType == "InStockType5") {
              this.inStockForm.inStockBillMxList.push({
                supplierCode: "",
                goodsCode: "",
                specificationCode: "",
                productionBatchCode: "",
                machineCode: "",
                goodsPrice: 0,
                materialList: [],
                inStockQuantity: 0,
                warehouseAreaCode: "",
                areaShelfCode: "",
                inStockRemark: "",
                rowKey: Date.now(),
              });
            }
            return false;
          }
          // this.inStockForm.inStockBillDetailList = this.inStockBillDetailList;
          const parmas = this.inStockForm;
            this.$api.warehouse
              .AddInStockBill(parmas)
              .then((res) => {
                let parameter = {
                  inStockBillNo: res,
                  inStockBillState: 3,
                  reason: "",
                };
                this.isAddPurDialog = false;
                this.$api.warehouse
                  .UpdateInStockBillState(parameter)
                  .then((res) => {
                    this.addPurLoading = false;
                    this.isAddPurDialog = false;
                    this.$message.success("保存成功")
                    this.getAllLists();
                  })
                  .catch((err) => {
                    this.addPurLoading = false;
                  });
                // this.addPurLoading = false;
                // this.isAddPurDialog = false;
                // this.getAllLists();
                // this.$message({
                //   message: "新建成功",
                //   type: "success",
                // });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
        }
      });
      }else{
        let parameter = {
          inStockBillNo: this.inStockForm.inStockBillNo,
          inStockBillState: 3,
          reason: "",
        };
        this.isAddPurDialog = false;
        this.$api.warehouse
          .UpdateInStockBillState(parameter)
          .then((res) => {
            this.addPurLoading = false;
            this.isAddPurDialog = false;
            this.$message.success("保存成功")
            this.getAllLists();
          })
          .catch((err) => {
            this.addPurLoading = false;
          });
      }
    },

    //红冲
    handleRedflush(row) {
      this.title = "红冲";
      this.inStockState.inStockBillNo = row.inStockBillNo;
      this.inStockState.inStockBillState = 4;
      this.reasonVisible = true;
    },

    //保存原因
    saveReason() {
      this.$refs.cancelForm.validate((valid) => {
        if (valid) {
          this.saveState();
        }
      });
    },

    //复制明细
    handleCopyRow(row, index) {
      this.inStockForm.inStockBillMxList.splice(
        index,
        0,
        JSON.parse(JSON.stringify(row))
      );
    },

    //删除明细
    handleDeleteRow(row, index) {
      this.inStockForm.inStockBillMxList.splice(index, 1);
    },

    //申请单双击事件
    confirmSelecd(row) {
      let state;
      if (row.billTypeName == "物料采购") {
        state = 1;
      } else {
        state = 2;
      }
      let obj = {
        type: state,
        associationBillNo: row.associationBillNo,
      };
      this.$api.warehouse
        .GetInStockRequestDetailBillList(obj)
        .then((res) => {
          this.inStockForm.associationBillNo = row.associationBillNo;
          this.inStockForm.requestUser = row.creator;
          this.inStockForm.requestUserCode = row.createBy;
          this.inStockForm.inStockType = row.billType;
          if (res.length > 0) {
            res.forEach((item, index) => {
              item.rowKey = index;
              this.$api.mater
                .GetGoodsSpecificationBySupplierCode({
                  queryBy: 0,
                  queryValue: item.supplierCode,
                })
                .then((data) => {
                  item.priceUnit = item.goodsUnit;
                  this.$set(item, "materialList", data);
                  item.isDisabled = true;
                  item.materialList.forEach((k) => {
                    //循环物料
                    if (item.goodsCode == k.goodsCode) {
                      this.$set(
                        item,
                        "goodsSpecificationList",
                        k.goodsSpecificationList
                      );
                      item.goodsSpecificationList.forEach((x) => {
                        //循环规格
                        if (item.specificationCode == x.specificationCode) {
                          this.$set(
                            item,
                            "productionBatchList",
                            x.productionBatchList
                          );
                          if (item.productionBatchList.length > 0) {
                            item.productionBatchList.forEach((j) => {
                              //循环批次
                              // if (
                              //   item.productionBatchCode == j.productionBatchCode
                              // ) {
                              //   item.productionDate = j.productionDate;
                              // } else {
                              // let obj = {
                              //   productionBatchCode: item.productionBatchCode,
                              //   productionBatchNumber:
                              //     item.productionBatchNumber,
                              //   productionDate: item.productionDate,
                              //   label: item.productionBatchNumber,
                              //   value: item.productionBatchCode,
                              // };
                              // item.productionBatchList.push(obj);
                              // }
                              j.label = j.productionBatchNumber;
                              j.value = j.productionBatchCode;
                            });
                          }
                          //  else {
                          //   let obj = {
                          //     productionBatchCode: item.productionBatchCode,
                          //     productionBatchNumber: item.productionBatchNumber,
                          //     productionDate: item.productionDate,
                          //     label: item.productionBatchNumber,
                          //     value: item.productionBatchCode,
                          //   };
                          //   item.productionBatchList.push(obj);
                          // }
                          item.goodsPrice = x.goodsPrice;
                        }
                      });
                      // item.goodsType = k.goodsType;
                      // item.goodsUnit = k.goodsUnit;
                    }
                  });
                });
            });
          }
          this.inStockForm.inStockBillMxList = res;
          // this.inStockForm.inStockBillMxList.push({
          //   supplierCode: "",
          //   goodsCode: "",
          //   specificationCode: "",
          //   productionBatchCode: "",
          //   materialList: [],
          //   machineCode: "",
          //   goodsPrice: 0,
          //   inStockQuantity: 0,
          //   warehouseAreaCode: "",
          //   areaShelfCode: "",
          //   isDisabled: false,
          //   inStockRemark: "",
          //   rowKey: Date.now(),
          // });
          this.isPurInfo = false;
          this.isAddPurDialog = true;
        })
        .catch(() => {});
    },

    // 库区库位
    getStorageLocations() {
      const parmas = {
        warehouseCode: this.inStockForm.warehouseCode,
      };
      this.$api.warehouse.GetStockQueryAreaTreeView(parmas).then((res) => {
        this.storageLocations = res;
      });
    },

    //点击生产单明细
    handleDetailClick(row) {
      let state;
      if (row.billTypeName == "物料采购") {
        state = 1;
      } else {
        state = 2;
      }
      let obj = {
        type: state,
        associationBillNo: row.associationBillNo,
      };
      this.detailLoading = true;
      this.$api.warehouse
        .GetInStockRequestDetailBillList(obj)
        .then((res) => {
          this.detail = res;
          this.detailLoading = false;
        })
        .catch(() => {
          this.detailLoading = false;
        });
    },

    //获取入库类型
    getInStockType() {
      this.$api.warehouse.GetGoodsInStockTypeDataList().then((res) => {
        this.inStockTypeList = res;
      });
    },

    // 入库新建
    newAddInstockBill() {
      this.state = "add";
      this.isPurInfo = true;
      this.disabled = false;
      // 采购单明细
      this.getPurInfo();
    },

    //其他入库
    handleOtherInstockBill() {
      this.inStockForm.inStockType = "InStockType5";
      this.state = "add";
      this.disabled = false;
      this.inStockForm.inStockBillMxList.push({
        supplierCode: "",
        goodsCode: "",
        specificationCode: "",
        productionBatchCode: "",
        machineCode: "",
        goodsPrice: 0,
        materialList: [],
        inStockQuantity: 0,
        warehouseAreaCode: "",
        areaShelfCode: "",
        inStockRemark: "",
        rowKey: Date.now(),
      });
      this.isAddPurDialog = true;
    },

    //保存生产批次
    handleSaveProductionBatch(row, e,index) {
      if (!row.productionDate) {
        this.$message.warning("请先选择生产日期");
        return;
      }
      let obj = {
        productionBatchID: 0,
        productionBatchCode: "",
        productionBatchNumber: e,
        productionDate: row.productionDate,
      };
      this.isShow = false
      this.$api.warehouse.AddProductionBatch(obj).then((res) => {
        res.label = res.productionBatchNumber;
        res.value = res.productionBatchCode;
        
        if (row.productionBatchList) {
          row.productionBatchList.push(res);
        } else {
          // row.productionBatchList = [];
          row.productionBatchList.push(res);
        }
        console.log(row)
        // row.productionBatchCode = res.productionBatchCode
        this.$set(row,'productionBatchCode', res.productionBatchCode)
        console.log(row)
        this.$message.success("保存成功");
        this.isShow = true
        this.$forceUpdate();
      });
    },

    //生产批次选择事件
    handleProductionBatchChange(row, e) {
      console.log(row);
      row.productionBatchList.forEach((item) => {
        if (item.productionBatchCode == e) {
          this.$set(row, "productionBatchNumber", item.productionBatchNumber);
          this.$set(row, "productionDate", item.productionDate);
          const newArr = [];
          newArr.push(item.warehouseAreaCode, item.areaShelfCode);
          item.stockArea = newArr;
        }
      });
    },

    //生产批次选择事件
    handleBatchChange(row, value) {
      // console.log(value)
      // return
      this.$set(row, "productionBatchNumber", value.productionBatchNumber);
      this.$set(row, "productionBatchCode", value.productionBatchCode);
      this.$set(row, "productionDate", value.productionDate);
      console.log(row);
    },

    //获取申请单
    getPurInfo() {
      this.$api.warehouse
        .GetPageInStockRequestBillList(this.purSearch)
        .then((res) => {
          this.purInfoLists = res.list;
          this.total = res.totalCount;
        });
    },

    // 入库单详情
    getStockBillInfo(id) {
      let obj = {
        queryBy: 0,
        queryValue: id,
      };
      this.$api.warehouse.GetSingleInStockBillView(obj).then((res) => {
        this.inStockForm = res;
        this.inStockForm.inStockBillMxList = res.inStockDetailList;
        if (this.inStockForm.inStockBillMxList.length) {
          this.inStockForm.inStockBillMxList.forEach((item,index) => {
            const newArr = [];
            newArr.push(item.warehouseAreaCode, item.areaShelfCode);
            item.rowKey =  index
            item.stockArea = newArr;
            this.$api.mater
              .GetGoodsSpecificationBySupplierCode({
                queryBy: 0,
                queryValue: item.supplierCode,
              })
              .then((data) => {
                item.materialList = data;
                item.materialList.forEach((k) => {
                  if (item.goodsCode == k.goodsCode) {
                    item.goodsSpecificationList = k.goodsSpecificationList;
                    item.priceUnit = k.goodsUnit;
                    item.goodsType = k.goodsType;
                    // item.goodsUnit = k.priceUnit;
                    item.goodsSpecificationList.forEach((x) => {
                      if (item.specificationCode == x.specificationCode) {
                        item.productionBatchList = x.productionBatchList;
                        if (item.productionBatchList.length > 0) {
                          item.productionBatchList.forEach((j) => {
                            if (
                              item.productionBatchCode == j.productionBatchCode
                            ) {
                              item.productionDate = j.productionDate;
                            } else {
                              let obj = {
                                productionBatchCode: item.productionBatchCode,
                                productionBatchNumber:
                                  item.productionBatchNumber,
                                productionDate: item.productionDate,
                                label: item.productionBatchNumber,
                                value: item.productionBatchCode,
                              };
                              item.productionBatchList.push(obj);
                            }
                            j.label = j.productionBatchNumber;
                            j.value = j.productionBatchCode;
                          });
                        } else {
                          let obj = {
                            productionBatchCode: item.productionBatchCode,
                            productionBatchNumber: item.productionBatchNumber,
                            productionDate: item.productionDate,
                            label: item.productionBatchNumber,
                            value: item.productionBatchCode,
                          };
                          item.productionBatchList.push(obj);
                        }
                        item.goodsPrice = x.goodsPrice;
                      }
                    });
                  }
                });
              });
          });
        }
        // if (!this.disabled) {
        //   this.inStockForm.inStockBillMxList.push({
        //     supplierCode: "",
        //     goodsCode: "",
        //     specificationCode: "",
        //     productionBatchCode: "",
        //     machineCode: "",
        //     goodsPrice: 0,
        //     inStockQuantity: 0,
        //     warehouseAreaCode: "",
        //     areaShelfCode: "",
        //     materialList: [],
        //     inStockRemark: "",
        //     rowKey: Date.now(),
        //   });
        // }
        this.isAddPurDialog = true;
      });
    },

    //保存状态
    saveState() {
      this.addLableLoading = true;
      this.$api.warehouse
        .UpdateInStockBillState(this.inStockState)
        .then((res) => {
          this.addPurLoading = false;
          this.isAddPurDialog = false;
          this.addLableLoading = false;
          this.reasonVisible = false;
          this.getAllLists();
          this.$message.success("保存成功");
        })
        .catch(() => {
          this.addPurLoading = false;
          this.isAddPurDialog = false;
          this.addLableLoading = false;
          this.reasonVisible = false;
        });
    },

    // 新建入库
    savaAdd() {
      this.$refs.inStockForm.validate((valid) => {
        if (valid) {
          this.addPurLoading = true;
          let isvalid = true;

          if (this.inStockForm.inStockType == "InStockType5") {
            let arr = JSON.parse(
              JSON.stringify(this.inStockForm.inStockBillMxList)
            );
            if (arr.length > 1) {
              arr.pop();
              arr.forEach((item) => {
                if (item.stockArea) {
                  item.warehouseAreaCode = item.stockArea[0];
                  item.areaShelfCode = item.stockArea[1];
                  if (item.stockArea.length < 1) {
                    isvalid = false;
                    return;
                  }
                } else {
                  isvalid = false;
                }
              });
            }
            this.inStockForm.inStockBillMxList = JSON.parse(
              JSON.stringify(arr)
            );
          } else {
            this.inStockForm.inStockBillMxList.forEach((item) => {
              if (item.stockArea) {
                item.warehouseAreaCode = item.stockArea[0];
                item.areaShelfCode = item.stockArea[1];
                if (item.stockArea.length < 1) {
                  isvalid = false;
                  return;
                }
              } else {
                isvalid = false;
              }
            });
          }
          console.log(isvalid);
          if (!isvalid) {
            this.$message({
              message: "请选择入库库位",
              type: "warning",
            });
            this.addPurLoading = false;
            if (this.inStockForm.inStockType == "InStockType5") {
              this.inStockForm.inStockBillMxList.push({
                supplierCode: "",
                goodsCode: "",
                specificationCode: "",
                productionBatchCode: "",
                machineCode: "",
                goodsPrice: 0,
                materialList: [],
                inStockQuantity: 0,
                warehouseAreaCode: "",
                areaShelfCode: "",
                inStockRemark: "",
                rowKey: Date.now(),
              });
            }
            return false;
          }
          // this.inStockForm.inStockBillDetailList = this.inStockBillDetailList;
          const parmas = this.inStockForm;
          if (this.state == "add") {
            this.$api.warehouse
              .AddInStockBill(parmas)
              .then(() => {
                this.addPurLoading = false;
                this.isAddPurDialog = false;
                this.getAllLists();
                this.$message({
                  message: "新建成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          } else if (this.state == "edit") {
            this.$api.warehouse
              .UpdateInStockBill(parmas)
              .then(() => {
                this.addPurLoading = false;
                this.isAddPurDialog = false;
                this.getAllLists();
                this.$message({
                  message: "编辑成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addPurLoading = false;
              });
          } else if (this.state == "confirm") {
            this.inStockState.inStockBillState = 2;
            this.saveState();
          } else if (this.state == "withdraw") {
            this.inStockState.inStockBillState = 1;
            this.saveState();
          } else if (this.state == "warehousing") {
            this.inStockState.inStockBillState = 3;
            this.saveState();
          }
        }
      });
    },
    // 给选中一行添加class
    tableRowClassName({ row }) {
      if (this.currentRow.purchaseOrderNo === row.purchaseOrderNo) {
        return "selectd";
      }
    },

    // 合计方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (column.property === "inStockSumValue") {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index]++;
          } else {
            sums[index] = " ";
          }
        }
      });
      return sums;
    },

    getDate() {
      return this.dayjs().format("YYYY-MM-DD");
    },
    // 获取仓库下拉列表
    getWarehouseList() {
      this.$api.warehouse.getWarehouseList().then((res) => {
        this.warhouseList = res;
        this.inStockForm.warehouseCode = res[0].warehouseCode;
        // 入库列表
        this.getAllLists();
      });
    },
    // 采购列表表格头部
    getTableHead() {
      const parmas = {
        tableCode: this.$config.InStockGoods,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
      });
    },
    // 入库列表
    getAllLists() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.warehouse
        .GetPageInStockBillList(parmas)
        .then((res) => {
          res.list.forEach((item) => {
            item.isFlush = 1;
            if (item.inStockBillState == 1) {
              item.inStockBillState = "待确认";
            } else if (item.inStockBillState == 2) {
              item.inStockBillState = "待入库";
            } else if (item.inStockBillState == 3) {
              item.inStockBillState = "已入库";
            } else if (item.inStockBillState == 4) {
              item.inStockBillState = "已红冲";
            } else {
              item.inStockBillState = "已作废";
            }

            if (
              item.inStockBillState == "已入库" &&
              item.inStockTypeName != "红冲入库"
            ) {
              item.isFlush = 0;
            }
          });
          this.tableData.tbody = res.list;
          this.search.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
    // 物料明细弹框关闭事件
    closeMarDialog() {
      this.materalSearch = this.$options.data().materalSearch;
    },
    // 关闭弹框事件
    closeDialog() {
      // 关闭弹框的时候清空数据
      if (this.$refs.inStockForm) {
        this.$refs.inStockForm.resetFields();
      }
      if (this.$refs.editInStockForm) {
        this.$refs.editInStockForm.resetFields();
      }
      this.inStockForm = this.$options.data().inStockForm;
      this.inStockBillDetailList = this.$options.data().inStockBillDetailList;
    },
  },
};
</script>
  <style lang="scss" scoped>
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}

.paginaCon.noborder {
  border-top: none;
  text-align: left;

  .pagination-container {
    padding: 0;
  }
}

.marDialog {
  height: 100%;

  .btnCon {
    border-bottom: none;
  }
}
</style>
